import {createStore} from 'vuex'
import {

    getPermissionApi
} from "../api/user.js";

export default createStore({
    state: {
        tagsLists: JSON.parse(sessionStorage.getItem('tagsLists') || '[]'),
        collapse: false,
        assetsTypeConfigList: [],
        storageTypeConfigList: [],
        materialTypeConfigList: [],
        permissionList: null,
        btnPermission: JSON.parse(sessionStorage.getItem('btnPermission') || '{}'),
        pagePermission: JSON.parse(sessionStorage.getItem('pagePermission') || '{}'),
        isAdministrator: false,
        projectObj: JSON.parse(sessionStorage.getItem('projectObj') || '{}')
    },
    mutations: {
        addAssetsTypeConfig(state, data) {
            state.assetsTypeConfigList = data;
        },
        addStorageTypeConfig(state, data) {
            state.storageTypeConfigList = data;
        },
        addMaterialTypeConfig(state, data) {
            state.materialTypeConfigList = data;
        },

        setBtnPermission(state, data) {
            state.btnPermission = data;
            sessionStorage.setItem('btnPermission', JSON.stringify(data));
        },
        setPagePermission(state, data) {
            state.pagePermission = data;
            sessionStorage.setItem('pagePermission', JSON.stringify(data));
        },
        isAdministratorFn(state, data) {
            state.isAdministrator = data;
            sessionStorage.setItem('isAdministrator', data);
        },


        delTagsItem(state, data) {
            state
                .tagsLists
                .splice(data.index, 1);
            sessionStorage.setItem('tagsLists',JSON.stringify(state.tagsLists))
        },
        setTagsItem(state, data) {
            state
                .tagsLists
                .push(data)

            sessionStorage.setItem('tagsLists',JSON.stringify(state.tagsLists))
        },
        clearTags(state) {
            state.tagsLists = [];
            sessionStorage.setItem('tagsLists',JSON.stringify(state.tagsLists))
        },
        closeTagsOther(state, data) {
            state.tagsLists = data;
            sessionStorage.setItem('tagsLists',JSON.stringify(state.tagsLists))
        },
        closeCurrentTag(state, data) {
            for (let i = 0, len = state.tagsLists.length; i < len; i++) {
                const item = state.tagsLists[i];
                if (item.path === data.$route.fullPath) {
                    if (i < len - 1) {
                        data
                            .$router
                            .push(state.tagsLists[i + 1].path);
                    } else if (i > 0) {
                        data
                            .$router
                            .push(state.tagsLists[i - 1].path);
                    } else {
                        data
                            .$router
                            .push("/");
                    }
                    state
                        .tagsLists
                        .splice(i, 1);
                    sessionStorage.setItem('tagsLists',JSON.stringify(state.tagsLists))
                    break;
                }


            }

        },
        // 侧边栏折叠
        handleCollapse(state, data) {
            state.collapse = data;
        },
        setPermissionList(state, data) {
            state.permissionList = data
        },
        setProjectObj(state,data){
            state.projectObj = data
            sessionStorage.setItem('projectObj',JSON.stringify(state.projectObj))
        }
    },
    actions: {
        getPermissionList(store, { userId, clientId }) {

            if (store.state.permissionList) {
                return new Promise((resolve, reject) => {
                    return resolve(store.state.permissionList || [])
                })
            }
            let parms = {
                clientId,
                userId
            }
            return getPermissionApi(parms).then((response) => {

                if (response.data && response.status == 200) {
                    let resData = response.data.data;
                    store.commit("setPermissionList", resData);
                    return resData
                } else {
                    return Promise.reject(response)
                }

            },).catch(() => {
                return Promise.resolve(store.state.permissionList || [])
            })
        },
    },
    modules: {}
})
